package alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class SentinelController {


    /**
     * 模拟下单接口
     *
     * @param pid 订单号
     * @return
     */
    @RequestMapping("/order/prod/{pid}")
    public Object order(@PathVariable("pid") Integer pid) {
        System.out.println("查询订单号");

        //模拟一次网络延时
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Map r = new HashMap();
        r.put("pid", pid);
        r.put("code", 0);
        r.put("message", "下单成功");
        return r;
    }

    @RequestMapping("/order/message")
    @SentinelResource(value = "/order/message",blockHandler = "aaa")
    public String message() {
        System.out.println("高并发下的问题测试");
        return "高并发下的问题测试";
    }

    public String aaa(BlockException e){
        return "被限流了！o(╥﹏╥)o";
    }


}
